//
//  SEDoctorTitleTextFieldCell.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/4/15.
//

import UIKit

struct SEDoctorTitleTextFieldCellModel {
    var title: String = ""
    var placeHolder: String = ""
    var cellType: SEDoctorTitleTextFieldCellType = .normal
    var unit: String = ""
    var segmentArray: [String] = []
    var segmentSelectedIndex: Int = 0
    var text: String = ""
    var editEnable: Bool = true
}

enum SEDoctorTitleTextFieldCellType {
    case normal
    case code
    case unit
    case segment
    case title
    case tap
}

protocol SEDoctorTitleTextFieldCellDelegate: NSObjectProtocol {
    func patientTitleTextFieldCellDidEndEdit(titleTextFieldCell: SEDoctorTitleTextFieldCell, indexPath: IndexPath)
}

class SEDoctorTitleTextFieldCell: UITableViewCell {

    static let className = "SEDoctorTitleTextFieldCell"
    static let reuseId = className

    @IBOutlet weak var textFieldTrailingToSuperView: NSLayoutConstraint!
    @IBOutlet weak var textFieldTrailingToCodeBtn: NSLayoutConstraint!
    @IBOutlet weak var textFieldTrailingToUnitLabel: NSLayoutConstraint!

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var textField: UITextField! {
        didSet {
            textField.delegate = self
        }
    }
    @IBOutlet weak var codeBtn: UIButton!
    @IBOutlet weak var unitLabel: UILabel!
    @IBOutlet weak var segmentControl: UISegmentedControl! {
        didSet {
            segmentControl.addTarget(self, action: #selector(segmentControlAction(_:)), for: .valueChanged)
        }
    }

    @IBAction func codeBtnAction(_ sender: UIButton) {
        print("code")
    }

    var cellType: SEDoctorTitleTextFieldCellType = .normal {
        didSet {
            textFieldTrailingToSuperView.priority = .defaultLow
            textFieldTrailingToCodeBtn.priority = .defaultLow
            textFieldTrailingToUnitLabel.priority = .defaultLow
            textField.isUserInteractionEnabled = true
            switch cellType {
            case .normal:
                codeBtn.isHidden = true
                unitLabel.isHidden = true
                textField.isHidden = false
                textField.isUserInteractionEnabled = model.editEnable
                segmentControl.isHidden = true
                textFieldTrailingToSuperView.priority = .defaultHigh
            case .code:
                codeBtn.isHidden = false
                unitLabel.isHidden = true
                textField.isHidden = false
                segmentControl.isHidden = true
                textFieldTrailingToCodeBtn.priority = .defaultHigh
            case .unit:
                codeBtn.isHidden = true
                unitLabel.isHidden = false
                textField.isHidden = false
                segmentControl.isHidden = true
                textFieldTrailingToUnitLabel.priority = .defaultHigh
            case .segment:
                codeBtn.isHidden = true
                unitLabel.isHidden = true
                textField.isHidden = true
                segmentControl.isHidden = false
            case .title:
                codeBtn.isHidden = true
                unitLabel.isHidden = true
                textField.isHidden = true
                segmentControl.isHidden = true
            case .tap:
                textField.isUserInteractionEnabled = false
                codeBtn.isHidden = true
                unitLabel.isHidden = true
                textField.isHidden = false
                segmentControl.isHidden = true
                textFieldTrailingToSuperView.priority = .defaultHigh
                let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(_:)))
                addGestureRecognizer(tap)
            }
        }
    }

    var indexPath = IndexPath(row: 0, section: 0)
    var model = SEDoctorTitleTextFieldCellModel()
    weak var delegate: SEDoctorTitleTextFieldCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
    }

    private func setUI() {
        cellType = .normal
    }

    func updateCell(model: SEDoctorTitleTextFieldCellModel, indexPath: IndexPath) {
        self.model = model
        self.indexPath = indexPath
        self.cellType = model.cellType
        self.titleLabel.text = model.title
        self.textField.placeholder = model.placeHolder
        self.textField.text = model.text
        self.unitLabel.text = model.unit
        if cellType == .segment {
            segmentControl.removeAllSegments()
            for item in model.segmentArray.reversed(){
                segmentControl.insertSegment(withTitle: item, at: 0, animated: false)
                segmentControl.setWidth(60, forSegmentAt: 0)
                segmentControl.selectedSegmentIndex = model.segmentSelectedIndex
            }
        }
    }

    @objc func segmentControlAction(_ segment: UISegmentedControl) {
        delegate?.patientTitleTextFieldCellDidEndEdit(titleTextFieldCell: self, indexPath: indexPath)
    }

    @objc func tapAction(_ tap: UITapGestureRecognizer) {
        delegate?.patientTitleTextFieldCellDidEndEdit(titleTextFieldCell: self, indexPath: indexPath)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

extension SEDoctorTitleTextFieldCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
        delegate?.patientTitleTextFieldCellDidEndEdit(titleTextFieldCell: self, indexPath: indexPath)
    }
}
